using System.Collections;
using System.Collections.Generic;
using BCW;
using Holoville.HOTween;
using Holoville.HOTween.Core;
using UnityEngine;

public class GameReputationUI : MonoBehaviour
{
	public static GameReputationUI instance;

	public GameObject reputationHolder;

	[SerializeField]
	private UIBasicSprite receivedRewardTexture;

	[SerializeField]
	private UIBasicSprite nextRewardTexture;

	[SerializeField]
	private UIBasicSprite nextNextRewardTexture;

	[SerializeField]
	private UIBasicSprite leagueTexture;

	[SerializeField]
	private Texture textureCash;

	[SerializeField]
	private Texture textureGold;

	[SerializeField]
	private UISprite glowReward;

	[SerializeField]
	public UILabel cupsCount;

	[SerializeField]
	private UILabel receivedRewardCount;

	[SerializeField]
	private GameObject rewardBar;

	[SerializeField]
	private GameObject rewardTexture;

	[SerializeField]
	private UILabel addReputationLabel;

	[SerializeField]
	private UILabel removeReputationLabel;

	[SerializeField]
	private GameObject addReputationGO;

	[SerializeField]
	private GameObject removeReputationGO;

	[SerializeField]
	private UISprite rewardGlowSprite;

	[SerializeField]
	private int shiftBar;

	[SerializeField]
	private float timeMoveBar = 1.2f;

	[SerializeField]
	private float timeMoveNewReward = 1.2f;

	[SerializeField]
	private Vector3 addRewardGlowStartPosition;

	[SerializeField]
	private Vector3 addRewardGlowEndPosition;

	[SerializeField]
	private Vector3 remRewardGlowStartPosition;

	[SerializeField]
	private Vector3 remRewardGlowEndPosition;

	[SerializeField]
	private UILabel receivedRewardLabel;

	[SerializeField]
	private UILabel nextRewardLabel;

	[SerializeField]
	private UILabel nextNextRewardLabel;

	[SerializeField]
	private float newRewardWindowTime;

	[SerializeField]
	private GameObject newRewardWindow;

	[SerializeField]
	private UIBasicSprite newRewardWindowTexture;

	[SerializeField]
	private UILabel newRewardWindowCount;

	[SerializeField]
	private GameObject lostRewardWindow;

	[SerializeField]
	private UIBasicSprite lostRewardWindowTexture;

	[SerializeField]
	private UILabel lostRewardWindowCount;

	[SerializeField]
	private GameObject buttonPrefab;

	[SerializeField]
	private GameObject rewardScroll;

	[SerializeField]
	private GameObject maxRewardGO;

	private bool enableBarAim;

	private int shiftToHideBar = 65;

	private bool nextRewardShow;

	private bool receivedTheAward = true;

	private float timeOfOccurrence = 1.2f;

	private string keyShowLeagueTutorial = "keyShowLeagueTutorial";

	private bool needShowBarAnim;

	private float needShowbarValue;

	private float needShowbarReputationChangeValue;

	[Header("Sound Key")]
	[SerializeField]
	private string keySoundReward;

	[SerializeField]
	private string keySoundLoseReward;

	[SerializeField]
	private string keySoundLoseReputation;

	[SerializeField]
	private string keySoundAddReputation;

	private List<GameObject> rewardButtons = new List<GameObject>();

	[SerializeField]
	private GameObject backGroundRewardScroll;

	private void Awake()
	{
		instance = this;
		TypeGame curTypeGame = GameController.instance.curTypeGame;
		if (curTypeGame == TypeGame.race)
		{
			cupsCount.gameObject.SetActive(false);
			leagueTexture.gameObject.SetActive(false);
		}
		else
		{
			SetLeague(CupsManager.CurrentLeague);
		}
		if (TDMController.isTeamGameType || curTypeGame == TypeGame.race || curTypeGame == TypeGame.TitanfallV2)
		{
			base.gameObject.SetActive(false);
		}
		else
		{
			SessionReputationManager.Instance.rewardChanged += RefreshRewardTexture;
			SessionReputationManager.Instance.reputationChanged += RefreshBar;
		}
		HOTween.Init();
		Init();
		cupsCount.text = CupsManager.Cups.ToString();
		CupsManager.cupsChanged += SetCups;
		CupsManager.leagueChanged += SetLeague;
		settings.gameModeChange += HideCupsUI;
		enableBarAim = true;
	}

	private void Start()
	{
		if (TutorialManager.TuturialWorking())
		{
			base.gameObject.SetActive(false);
			cupsCount.transform.parent.gameObject.SetActive(false);
			OnDestroy();
		}
	}

	private void SetCups(int value)
	{
		cupsCount.text = value.ToString();
	}

	private void SetLeague(League league)
	{
		leagueTexture = leagueTexture.ReplaceForCurrentPlatform();
		leagueTexture.LoadTextureAndAlphaMask("LigasIcons/", league.ToString());
		string text = BCWStorage.Get<string>("new_tutorial");
		int num = BCWStorage.Get<int>(keyShowLeagueTutorial);
		if (num == 1 && !text.Equals("OnlineReputation") && !text.Equals("Start"))
		{
			if (!settings.offlineMode)
			{
				CoroutineRunner.Instance.StartCoroutine(WaitGameTop());
			}
		}
		else if (num == 0)
		{
			BCWStorage.Set(keyShowLeagueTutorial, 1);
		}
	}

	private IEnumerator WaitGameTop()
	{
		while (GameTopWindow.instance == null)
		{
			yield return null;
		}
		yield return new WaitForSeconds(1f);
		InGameTutorial.StartTutorial("new_tutorial_League", GameTopWindow.instance.gameObject.transform.parent, string.Empty);
		BCWStorage.Set(keyShowLeagueTutorial, 2);
	}

	private void RefreshBar(float barValue, float reputationChangeValue)
	{
		if (!enableBarAim)
		{
			needShowbarValue = barValue;
			needShowbarReputationChangeValue = reputationChangeValue;
			needShowBarAnim = true;
			return;
		}
		needShowBarAnim = false;
		if (reputationChangeValue >= 0f)
		{
			BCWAudioResource.Instance.PlaySound(keySoundAddReputation);
		}
		else
		{
			BCWAudioResource.Instance.PlaySound(keySoundLoseReputation);
		}
		MoveBar(new Vector3((float)shiftBar - (float)shiftBar * barValue * 2f, 0f, 0f), reputationChangeValue);
	}

	private void MoveBar(Vector3 newPosition, float reputationChangeValue)
	{
		if (!nextRewardShow)
		{
			if (receivedTheAward)
			{
				Vector3 vector = new Vector3(shiftBar, 0f, 0f);
				HOTween.Kill(rewardTexture.transform);
				HOTween.Kill(rewardBar.transform);
				HOTween.To(rewardTexture.transform, timeOfOccurrence, new TweenParms().Prop("localPosition", vector).UpdateType(UpdateType.TimeScaleIndependentUpdate));
				HOTween.To(rewardBar.transform, timeOfOccurrence, new TweenParms().Prop("localPosition", vector).UpdateType(UpdateType.TimeScaleIndependentUpdate).OnComplete((TweenDelegate.TweenCallback)delegate
				{
					nextNextRewardTexture.gameObject.SetActive(true);
					Move(timeMoveBar, newPosition, reputationChangeValue);
				}));
			}
			else
			{
				Vector3 vector2 = new Vector3(-shiftBar, 0f, 0f);
				HOTween.Kill(rewardTexture.transform);
				HOTween.Kill(rewardBar.transform);
				HOTween.To(rewardTexture.transform, timeOfOccurrence, new TweenParms().Prop("localPosition", vector2).UpdateType(UpdateType.TimeScaleIndependentUpdate));
				HOTween.To(rewardBar.transform, timeOfOccurrence, new TweenParms().Prop("localPosition", vector2).UpdateType(UpdateType.TimeScaleIndependentUpdate).OnComplete((TweenDelegate.TweenCallback)delegate
				{
					nextNextRewardTexture.gameObject.SetActive(true);
					Move(timeMoveBar, newPosition, reputationChangeValue);
				}));
			}
		}
		else
		{
			Move(timeMoveBar, newPosition, reputationChangeValue);
		}
		nextRewardShow = true;
	}

	private void Move(float time, Vector3 newPosition, float reputationChangeValue)
	{
		if (reputationChangeValue > 0f)
		{
			addReputationLabel.text = "+" + reputationChangeValue;
			addReputationLabel.gameObject.SetActive(true);
			addReputationLabel.gameObject.transform.localScale = Vector3.one * 0.75f;
			HOTween.Kill(addReputationLabel.gameObject.transform);
			HOTween.To(addReputationLabel.gameObject.transform, timeMoveBar / 2f, new TweenParms().Prop("localScale", Vector3.one).UpdateType(UpdateType.TimeScaleIndependentUpdate).OnComplete((TweenDelegate.TweenCallback)delegate
			{
				HOTween.To(addReputationLabel.gameObject.transform, timeMoveBar / 2f, new TweenParms().Prop("localScale", Vector3.one * 0.75f).UpdateType(UpdateType.TimeScaleIndependentUpdate));
			}));
			addReputationGO.transform.localPosition = new Vector3(120f, 0f, 0f);
			HOTween.Kill(addReputationGO.transform);
			HOTween.To(addReputationGO.transform, timeMoveBar, new TweenParms().Prop("localPosition", new Vector3(-60f, 0f, 0f)).UpdateType(UpdateType.TimeScaleIndependentUpdate));
			removeReputationLabel.gameObject.SetActive(false);
			glowReward.color = Color.green;
		}
		else
		{
			removeReputationLabel.text = "-" + Mathf.Abs(reputationChangeValue);
			removeReputationLabel.gameObject.SetActive(true);
			removeReputationLabel.gameObject.transform.localScale = Vector3.one * 0.75f;
			HOTween.Kill(removeReputationLabel.gameObject.transform);
			HOTween.To(removeReputationLabel.gameObject.transform, timeMoveBar / 2f, new TweenParms().Prop("localScale", Vector3.one).UpdateType(UpdateType.TimeScaleIndependentUpdate).OnComplete((TweenDelegate.TweenCallback)delegate
			{
				HOTween.To(removeReputationLabel.gameObject.transform, timeMoveBar / 2f, new TweenParms().Prop("localScale", Vector3.one * 0.75f).UpdateType(UpdateType.TimeScaleIndependentUpdate));
			}));
			removeReputationGO.transform.localPosition = new Vector3(-120f, 0f, 0f);
			HOTween.Kill(removeReputationGO.transform);
			HOTween.To(removeReputationGO.transform, timeMoveBar, new TweenParms().Prop("localPosition", new Vector3(60f, 0f, 0f)).UpdateType(UpdateType.TimeScaleIndependentUpdate));
			addReputationLabel.gameObject.SetActive(false);
			glowReward.color = Color.red;
		}
		if (!nextNextRewardTexture.gameObject.activeSelf)
		{
			nextNextRewardTexture.gameObject.SetActive(true);
		}
		HOTween.Kill(rewardBar.transform);
		HOTween.Kill(glowReward.transform);
		HOTween.Kill(rewardTexture.transform);
		glowReward.gameObject.SetActive(true);
		glowReward.transform.localPosition = rewardBar.transform.localPosition;
		HOTween.To(rewardTexture.transform, timeMoveBar, new TweenParms().Prop("localPosition", newPosition).UpdateType(UpdateType.TimeScaleIndependentUpdate));
		HOTween.To(glowReward.transform, timeMoveBar, new TweenParms().Prop("localPosition", newPosition).UpdateType(UpdateType.TimeScaleIndependentUpdate));
		HOTween.To(rewardBar.transform, timeMoveBar, new TweenParms().Prop("localPosition", newPosition).UpdateType(UpdateType.TimeScaleIndependentUpdate).OnComplete((TweenDelegate.TweenCallback)delegate
		{
			removeReputationLabel.gameObject.SetActive(false);
			addReputationLabel.gameObject.SetActive(false);
			glowReward.gameObject.SetActive(false);
		}));
	}

	private void OnDestroy()
	{
		SessionReputationManager.Instance.rewardChanged -= RefreshRewardTexture;
		SessionReputationManager.Instance.reputationChanged -= RefreshBar;
		CupsManager.cupsChanged -= SetCups;
		CupsManager.leagueChanged -= SetLeague;
		settings.gameModeChange -= HideCupsUI;
	}

	public Texture GetTexture(SessionReputationReward reward)
	{
		if (reward.type == SessionReputationRewardType.Cash)
		{
			return textureCash;
		}
		if (reward.type == SessionReputationRewardType.Gold)
		{
			return textureGold;
		}
		return MarketController.Instance.GetProductTexture(reward.rewardID);
	}

	private void SetTextureAndCount(SessionReputationReward reward, ref UIBasicSprite target, UILabel count)
	{
		if (reward == null)
		{
			target.mainTexture = null;
			count.gameObject.SetActive(false);
			return;
		}
		target = target.ReplaceForCurrentPlatform();
		switch (reward.type)
		{
		case SessionReputationRewardType.Product:
			target.LoadTextureAndAlphaMask("AtlasShopGuns/", MarketController.Instance.GetProduct(reward.rewardID).texture);
			break;
		case SessionReputationRewardType.Cash:
			target.LoadTextureAndAlphaMask("NGUI_Textures/", "cash");
			break;
		case SessionReputationRewardType.Gold:
			target.LoadTextureAndAlphaMask("NGUI_Textures/", "gold");
			break;
		}
		count.text = reward.rewardCount.ToString();
		count.gameObject.SetActive(reward.rewardCount > 0);
	}

	private void RefreshRewardTexture(bool addReward)
	{
		addReputationLabel.gameObject.SetActive(false);
		removeReputationLabel.gameObject.SetActive(false);
		glowReward.gameObject.SetActive(false);
		SessionReputationReward receivedReward = null;
		SessionReputationReward nextReward = null;
		SessionReputationReward nextNextReward = null;
		int rRewardCount = 0;
		int nextRewardCount = 0;
		SessionReputationManager.Instance.GetReceivedReward(ref receivedReward, ref nextReward, ref nextNextReward, ref rRewardCount, ref nextRewardCount);
		enableBarAim = false;
		if (addReward)
		{
			ShowNewReward(receivedReward);
			receivedRewardTexture.transform.parent.gameObject.SetActive(rRewardCount > 0);
			SetTextureAndCount(null, ref receivedRewardTexture, receivedRewardLabel);
			HOTween.Kill(rewardBar.transform);
			HOTween.To(rewardBar.transform, timeMoveNewReward, new TweenParms().Prop("localPosition", new Vector3(-shiftBar - shiftToHideBar, 0f, 0f)).UpdateType(UpdateType.TimeScaleIndependentUpdate));
			HOTween.Kill(rewardTexture.transform);
			HOTween.To(rewardTexture.transform, timeMoveNewReward, new TweenParms().Prop("localPosition", new Vector3(-shiftBar - shiftToHideBar, 0f, 0f)).UpdateType(UpdateType.TimeScaleIndependentUpdate).OnComplete((TweenDelegate.TweenCallback)delegate
			{
				BCWAudioResource.Instance.PlaySound(keySoundReward);
				rewardBar.transform.localPosition = new Vector3(shiftBar + shiftToHideBar, 0f, 0f);
				rewardTexture.transform.localPosition = new Vector3(shiftBar + shiftToHideBar, 0f, 0f);
				SetTextureAndCount(receivedReward, ref receivedRewardTexture, receivedRewardLabel);
				SetTextureAndCount(nextReward, ref nextRewardTexture, nextRewardLabel);
				SetTextureAndCount(nextNextReward, ref nextNextRewardTexture, nextNextRewardLabel);
				if (nextNextReward == null)
				{
					nextNextRewardTexture.transform.parent.gameObject.SetActive(false);
				}
				nextNextRewardTexture.gameObject.SetActive(false);
				nextRewardShow = false;
				enableBarAim = true;
				receivedTheAward = true;
				receivedRewardCount.text = rRewardCount.ToString();
				receivedRewardCount.gameObject.SetActive(rRewardCount > 1);
				if (nextReward == null)
				{
					LockBarAllRewardRecieved();
				}
				else if (needShowBarAnim)
				{
					RefreshBar(needShowbarValue, needShowbarReputationChangeValue);
				}
			}));
		}
		else
		{
			ShowLostReward(nextReward);
			SetTextureAndCount(null, ref nextNextRewardTexture, nextNextRewardLabel);
			HOTween.Kill(rewardBar.transform);
			HOTween.To(rewardBar.transform, timeMoveNewReward, new TweenParms().Prop("localPosition", new Vector3(shiftBar + shiftToHideBar, 0f, 0f)).UpdateType(UpdateType.TimeScaleIndependentUpdate));
			HOTween.Kill(rewardTexture.transform);
			HOTween.To(rewardTexture.transform, timeMoveNewReward, new TweenParms().Prop("localPosition", new Vector3(shiftBar + shiftToHideBar, 0f, 0f)).UpdateType(UpdateType.TimeScaleIndependentUpdate).OnComplete((TweenDelegate.TweenCallback)delegate
			{
				BCWAudioResource.Instance.PlaySound(keySoundLoseReward);
				receivedRewardTexture.transform.parent.gameObject.SetActive(rRewardCount > 0);
				rewardBar.transform.localPosition = new Vector3(-shiftBar - shiftToHideBar, 0f, 0f);
				rewardTexture.transform.localPosition = new Vector3(-shiftBar - shiftToHideBar, 0f, 0f);
				SetTextureAndCount(receivedReward, ref receivedRewardTexture, receivedRewardLabel);
				SetTextureAndCount(nextNextReward, ref nextNextRewardTexture, nextNextRewardLabel);
				SetTextureAndCount(nextReward, ref nextRewardTexture, nextRewardLabel);
				nextRewardShow = false;
				receivedTheAward = false;
				enableBarAim = true;
				receivedRewardCount.text = rRewardCount.ToString();
				receivedRewardCount.gameObject.SetActive(rRewardCount > 1);
				if (needShowBarAnim)
				{
					RefreshBar(needShowbarValue, needShowbarReputationChangeValue);
				}
			}));
		}
		if (rewardScroll.activeSelf)
		{
			FillRewardList();
		}
	}

	private void Init()
	{
		leagueTexture = leagueTexture.ReplaceForCurrentPlatform();
		SessionReputationReward receivedReward = null;
		SessionReputationReward nextReward = null;
		SessionReputationReward nextNextReward = null;
		int num = 0;
		int nextRewardCount = 0;
		SessionReputationManager.Instance.GetReceivedReward(ref receivedReward, ref nextReward, ref nextNextReward, ref num, ref nextRewardCount);
		SetTextureAndCount(receivedReward, ref receivedRewardTexture, receivedRewardLabel);
		SetTextureAndCount(nextReward, ref nextRewardTexture, nextRewardLabel);
		SetTextureAndCount(nextNextReward, ref nextNextRewardTexture, nextNextRewardLabel);
		nextNextRewardTexture.gameObject.SetActive(false);
		receivedRewardCount.gameObject.SetActive(num > 1);
		Vector3 localPosition = new Vector3(shiftBar + shiftToHideBar, 0f, 0f);
		rewardBar.transform.localPosition = localPosition;
		rewardTexture.transform.localPosition = localPosition;
		receivedRewardTexture.transform.parent.gameObject.SetActive(false);
	}

	private void ShowNewReward(SessionReputationReward reward)
	{
		SetTextureAndCount(reward, ref newRewardWindowTexture, newRewardWindowCount);
		newRewardWindow.SetActive(true);
		Invoke("HideNewReward", newRewardWindowTime);
	}

	private void HideNewReward()
	{
		newRewardWindow.SetActive(false);
	}

	private void ShowLostReward(SessionReputationReward reward)
	{
		SetTextureAndCount(reward, ref lostRewardWindowTexture, lostRewardWindowCount);
		lostRewardWindow.SetActive(true);
		Invoke("HideLostReward", newRewardWindowTime);
	}

	private void HideLostReward()
	{
		lostRewardWindow.SetActive(false);
	}

	private void HideCupsUI()
	{
		if (cupsCount != null)
		{
			cupsCount.transform.parent.gameObject.SetActive(!settings.offlineMode);
		}
	}

	private void Compress(List<SessionReputationReward> list)
	{
		for (int i = 0; i < list.Count; i++)
		{
			for (int j = i + 1; j < list.Count; j++)
			{
				if (list[i].type == list[j].type && list[i].rewardID == list[j].rewardID)
				{
					list[i].rewardCount = list[i].rewardCount + list[j].rewardCount;
					list.RemoveAt(j--);
				}
			}
		}
	}

	private void FillRewardList()
	{
		List<SessionReputationReward> list = new List<SessionReputationReward>();
		foreach (SessionReputationReward reward in SessionReputationManager.Instance.GetRewardList())
		{
			SessionReputationReward sessionReputationReward = new SessionReputationReward();
			sessionReputationReward.type = reward.type;
			sessionReputationReward.rewardID = reward.rewardID;
			sessionReputationReward.rewardCount = reward.rewardCount;
			list.Add(sessionReputationReward);
		}
		Compress(list);
		int i;
		for (i = 0; i < list.Count; i++)
		{
			GameObject gameObject;
			if (rewardButtons.Count < i + 1)
			{
				gameObject = Object.Instantiate(buttonPrefab);
				gameObject.transform.SetParent(buttonPrefab.transform.parent);
				gameObject.transform.localScale = Vector3.one;
				rewardButtons.Add(gameObject);
			}
			else
			{
				gameObject = rewardButtons[i];
			}
			UIBasicSprite target = gameObject.GetComponentInChildren<UIBasicSprite>();
			UILabel componentInChildren = gameObject.GetComponentInChildren<UILabel>();
			SetTextureAndCount(list[i], ref target, componentInChildren);
			gameObject.SetActive(true);
			gameObject.transform.localPosition = new Vector3(70 * i, 0f, 0f);
		}
		for (; i < rewardButtons.Count; i++)
		{
			rewardButtons[i].SetActive(false);
		}
	}

	public void ToggleRewardPanel()
	{
		if (!rewardScroll.activeSelf)
		{
			FillRewardList();
		}
		rewardScroll.SetActive(!rewardScroll.activeSelf);
		backGroundRewardScroll.SetActive(!backGroundRewardScroll.activeSelf);
	}

	private void LockBarAllRewardRecieved()
	{
		maxRewardGO.SetActive(true);
		nextNextRewardTexture.transform.parent.gameObject.SetActive(false);
		rewardBar.SetActive(false);
	}
}
